<?php
/*************************************************************************
 * Zero PHP
 * Copyright (c) 2012
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * Author: zlk
 *************************************************************************/
 class Run{

 	//框架初始化入口
 	public static function init(){
 		
 		 // 设定错误和异常处理
        set_error_handler(array('Run','customError'));
        set_exception_handler(array('Run','customException'));
        // 设置系统时区 PHP5支持
        if(function_exists('date_default_timezone_set'))
            date_default_timezone_set(C('zero.Time_Zone'));
         // Session初始化
        if(C('zero.Session'))  session_start();
        // URL调度,设置module和action
        $dispatch=C('util.dispatch')?C('util.dispatch'):'Idispatcher';
        CZero::instance($dispatch,'init');
        self::exec();
 	}
 	
 	//框架执行方法
 	public static function exec(){
 		 // 安全检测
 		if(GROUP_NAME&&!preg_match('/^[A-Za-z_0-9]+$/',GROUP_NAME)){
            exit('group exception');
        }
        if(!preg_match('/^[A-Za-z_0-9]+$/',MODULE_NAME)){
            exit('module exception');
        }
        if(!preg_match('/^[A-Za-z_0-9]+$/',ACTION_NAME)){
            exit('action exception');
        }
        //判断组
         $group =  GROUP_NAME ? GROUP_NAME.'.' : '';
         $module  = CZero::newAction($group.MODULE_NAME);
         if(!is_object($module)) throw new Exception($module.'not exist');
         $action=ACTION_NAME;
         //执行操作
         if(method_exists($module,$action)){
         $module->$action();
         }else{
         	throw new Exception($action.'not exist');
         }
 	}
 	
 	//自定义错误处理
 	public static function customError($errno, $errstr, $errfile, $errline){
 		switch ($errno) {
          case 4096:
            $errorStr = "[Fatal error] '$errstr' in file ".basename($errfile)." Fatal error on line $errline.";
            break;
          case 2:
          	$errorStr = "[Warning] '$errstr' in file ".$errfile." Warning on line $errline.";
          	break;
          case 8:
          	$errorStr = "[Notice] '$errstr' in file ".basename($errfile)." Notice on line $errline.";
          	break;
          default:
            $errorStr = "Unknown error type [$errno] '$errstr' in file ".basename($errfile)." error on line $errline.";
            break;
      }
      echo '<pre>'.$errorStr.'</pre>';
      return true;
 	}
 	
 	//自定义异常处理
 	public static function customException($exception){
 		echo "<b>Exception:</b> " , $exception->getMessage();
 	}
 	
 }
?>
